package com.eascs.crm.nsEditDetail.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.AssetsTotInfoEntity;
import com.eascs.crm.nsEditDetail.form.AssetsTotInfoForm;
import com.eascs.crm.nsEditDetail.service.AssetsTotInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

/**
 * 夫妻双方资产负债
 * 
 * @author lwx
 *
 */
@Controller
@RequestMapping("/tot_assets")
public class AssetsTotInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(AssetsTotInfoRestController.class);

	@Inject
	AssetsTotInfoService assetsInfoService;

	// 新增修改
	@RequestMapping(value = "/tot_assets_op")
	public HtmlRestResult assetsOp(String loanapplyId) {
		AssetsTotInfoEntity vo = new AssetsTotInfoEntity();
		if (StringUtil.isNotBlank(loanapplyId)) {
			vo = assetsInfoService.findAssetsTotInfoByLoanapplyId(loanapplyId);
		}
		ViewContext.put("vo", vo);
		ViewContext.put("loanapplyId", loanapplyId);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/tot_assets/tot_assets_op");
	}

	@RequestMapping("/tot_assets_save")
	public BaseRestResult<String> assetsSave(AssetsTotInfoForm form) {
		AssetsTotInfoEntity assets = assetsInfoService.findAssetsTotInfoByLoanapplyId(form.getLoanapplyId());
		boolean flag = false;
		if (assets != null) {// 修改
			assets.setAssetsPrice(form.getAssetsPrice());
			assets.setAssetsPs(form.getAssetsPs());
			assets.setLiabiTot(form.getLiabiTot());
			assets.setPs(form.getPs());
			assets.setBankPrice(form.getBankPrice());
			assets.setTotPrice(form.getTotPrice());
			assets.setCarPrice(form.getCarPrice());
			assets.setNobankPrice(form.getNobankPrice());
			assets.setMjPrice(form.getMjPrice());
			assets.setDpPrice(form.getDpPrice());
			flag = assetsInfoService.assetsUpdate(assets);
		} else {// 新增
			assets = new AssetsTotInfoEntity();
			assets.setAssetsPrice(form.getAssetsPrice());
			assets.setAssetsPs(form.getAssetsPs());
			assets.setLiabiTot(form.getLiabiTot());
			assets.setPs(form.getPs());
			assets.setBankPrice(form.getBankPrice());
			assets.setTotPrice(form.getTotPrice());
			assets.setCarPrice(form.getCarPrice());
			assets.setNobankPrice(form.getNobankPrice());
			assets.setMjPrice(form.getMjPrice());
			assets.setDpPrice(form.getDpPrice());
			assets.setLoanapplyNo(form.getLoanapplyId());
			flag = assetsInfoService.assetsSave(assets);
		}
		if (flag) {
			return BaseRestResult.info("操作成功").andRefresh();
		} else {
			return BaseRestResult.warn("操作失败");
		}
	}

	// 异步加载详情
	@RequestMapping(value = "/tot_assets_show", method = RequestMethod.GET)
	public HtmlRestResult assetsShow(String loanapplyId) {
		AssetsTotInfoEntity vo = new AssetsTotInfoEntity();
		if (StringUtil.isNotBlank(loanapplyId)) {
			vo = assetsInfoService.findAssetsTotInfoByLoanapplyId(loanapplyId);
		}
		ViewContext.put("vo", vo);
		return new HtmlRestResult("/nsEditDetail/tot_assets/tot_assets_show");
	}


}
